//
//  main.m
//  07-Homework
//
//  Created by Snow on 16/7/12.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "Zone.h"
#import "TempCity.h"

int main(int argc, const char * argv[]) {
    //1.省市区类方法
    NSString *path = @"/Users/Snow/DLS160710/Objective-C/HomeWork/07-Homework/07-Homework/area.txt";
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    NSMutableArray *allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@""]) {
            Province *pro = [Province zoneWithName:area];
            [allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [allProArray lastObject];
            City *city = [City zoneWithName:@"area"];
            [pro.array addObject:city];
        } else {
            City *city = [[[allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", allProArray);
    
    // 2.
    NSString *path1 = @"/Users/Snow/DLS160710/Objective-C/HomeWork/07-Homework/07-Homework/Location1.txt";
    NSString *source = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil];
    NSArray *allCityArray = [source componentsSeparatedByString:@","];
    NSMutableArray *allTempCityArray = [NSMutableArray array];
    for (NSString *cityString in allCityArray) {
        NSArray *city = [cityString componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:[city firstObject] number:[city lastObject]];
        [allTempCityArray addObject:tempCity];
        [tempCity release];
    }
    //排序
    NSSortDescriptor *sortTempCity = [[NSSortDescriptor alloc] initWithKey:@"cityName" ascending:YES];
    [allTempCityArray sortUsingDescriptors:@[sortTempCity]];
    [sortTempCity release];
    NSMutableArray *finalClassArray = [NSMutableArray array];
    for (TempCity *temp in allTempCityArray) {
        if ([temp.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [Province zoneWithTempCity:temp];
            [finalClassArray addObject:pro];
        } else if ([temp.cityNumber hasSuffix:@"00"] && ![temp.cityNumber hasSuffix:@"0000"]) {
            Province *pro = [finalClassArray lastObject];
            City *city = [City zoneWithTempCity:temp];
            [pro.array addObject:city];
        } else {
            Province *pro = [finalClassArray lastObject];
            City *city = [pro.array lastObject];
            Zone *zone = [Zone zoneWithTempCity:temp];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", finalClassArray);
    
    return 0;
}
